package com.tvein.sanguo.room.view
{
	//flash
	import flash.net.LocalConnection;
	//pureMVC
	import org.puremvc.as3.multicore.interfaces.INotification;
	import org.puremvc.as3.multicore.utilities.fabrication.components.FlashApplication;
	import org.puremvc.as3.multicore.utilities.fabrication.patterns.mediator.FlashMediator;	
	//custom
	import com.tvein.sanguo.room.common.GameConstants;
	import com.tvein.sanguo.Room;
	import com.tvein.sanguo.room.controller.RoomInit;
	import com.tvein.sanguo.room.controller.RoomFile;
	import com.tvein.sanguo.room.controller.RoomBuild;
	import com.tvein.sanguo.room.controller.RoomClear;
	import com.tvein.sanguo.room.controller.RoomMessage;
	import com.tvein.sanguo.room.controller.RoomScript;
//
	public class SystemMediator extends FlashMediator
	{
		public static const NAME : String = 'SystemMediator';
		
		public function SystemMediator(viewComponent:Object)
		{
			super(NAME, viewComponent);
		}
		
		public function get room() : Room
		{
			return viewComponent as Room;
		}
		
		//Custom
		override public function onRegister() : void
        {
			facade.registerCommand(GameConstants.ROOM_INIT, RoomInit);
			facade.registerCommand(GameConstants.ROOM_FILE, RoomFile);
			facade.registerCommand(GameConstants.ROOM_BUILD, RoomBuild);
			facade.registerCommand(GameConstants.ROOM_CLEAR, RoomClear);
			facade.registerCommand(GameConstants.ROOM_MESSAGE, RoomMessage);
			facade.registerCommand(GameConstants.ROOM_SCRIPT, RoomScript);
        }
		
		override public function listNotificationInterests() : Array
		{
			var interests:Array = super.listNotificationInterests();
			interests.push(GameConstants.MODULE_INVOKE);
			interests.push(GameConstants.GAME_CONSOLE);
			return interests;
		}
		
		override public function handleNotification(notification:INotification) : void
		{
			switch(notification.getName())
			{
				case GameConstants.MODULE_INVOKE:
					trace(GameConstants.MODULE_ROOM + " Invoke : " + notification.getBody()[0]);
					switch(notification.getBody()[0])
					{
						case "-init":
							sendNotification(GameConstants.ROOM_INIT, notification.getBody()[1]);
							break;
						case "-file":
							sendNotification(GameConstants.ROOM_FILE, notification.getBody()[1]);
							break;
						case "-build":
							sendNotification(GameConstants.ROOM_BUILD, notification.getBody()[1]);
							break;
						case "-clear":
							sendNotification(GameConstants.ROOM_CLEAR, notification.getBody()[1]);
							break;
						case "-message":
							sendNotification(GameConstants.ROOM_MESSAGE, notification.getBody()[1]);
							break;
						case "-script":
							sendNotification(GameConstants.ROOM_SCRIPT, notification.getBody()[1]);
							break;
					}
					break;
			}
			
			try
			{
				new LocalConnection().connect("Tvein");
				new LocalConnection().connect("Tvein");
			}
			catch (error : Error)
			{
			
			}
		}
    }
}